package org.springblade.task.job.report.user;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.common.utils.DateUtils;
import org.springblade.modules.userAnalysis.entity.DailyActiveUsersReport;
import org.springblade.modules.userAnalysis.entity.MonthActiveUsersReport;
import org.springblade.modules.userAnalysis.service.IMonthActiveUsersReportService;
import org.springblade.task.base.param.BaseUserParam;
import org.springblade.task.base.service.IBaseUserVisitLogService;
import org.springblade.task.base.service.IBaseVipUserService;
import org.springblade.task.job.base.BaseTaskInfoJob;
import org.springblade.task.quartz.entity.QuartzTask;
import org.springblade.task.quartz.service.IQuartzTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: 月活跃用户
 * @version V1.0
 */
@Component
public class MonthActiveUsersReportJob {

	private static Logger logger = LoggerFactory.getLogger(BaseTaskInfoJob.class);

	@Autowired
	private IQuartzTaskService quartzTaskService;

	@Autowired
	private IBaseUserVisitLogService baseUserVisitLogService;
	@Autowired
	private IBaseVipUserService baseVipUserService;
	@Autowired
	private IMonthActiveUsersReportService monthActiveUsersReportService;

	/**
	 * 1、简单任务示例（Bean模式）
	 */
	@XxlJob("monthActiveUsersReportJob")
	public ReturnT<String> monthActiveUsersReportJob(String param) throws Exception {
		System.out.println(DateUtils.currentDateTime() + " -- XXL-JOB, Hello World.");
		System.err.println("执行静态定时任务日期: " + MonthActiveUsersReportJob.class.getSimpleName());
		QuartzTask quartzTask = quartzTaskService.queryByTaskName(MonthActiveUsersReportJob.class.getSimpleName(), -7);
		// 今日
		String todayDate = DateUtils.crutDate();
		String taskDate = quartzTask.getTaskDate();
		while (taskDate.compareTo(todayDate) < 0) {
			// 当时间大于5点，跳出执行
//			if (DateUtils.getHour24() > 5) {
//				break;
//			}
			// 再加一个凌晨1点判断
			currMethod(taskDate);
			// 增加1天
			taskDate = DateUtils.dateAddDay(taskDate, 1);
		}
		quartzTask.setTaskDate(taskDate);
		quartzTaskService.saveQuartzTask(quartzTask);
		return ReturnT.SUCCESS;
	}

	/**
	 *
	 * @param taskDate
	 */
	public void currMethod(String taskDate) {

		try {
			int year = DateUtils.getYear(new Date());
			int month = DateUtils.getMonth(new Date());
			String firstDayOfMonth = DateUtils.DateYMDhhmmssToString(DateUtils.getFirstDayOfMonth(year, month));
			BaseUserParam param = new BaseUserParam();
			param.setStartDate(firstDayOfMonth);
			param.setEndDate(DateUtils.getDayEndTime(taskDate));
			List<String> loginUserList = baseUserVisitLogService.queryByTodayUserList(param);
			List<String> vipUserList = baseVipUserService.queryTodayVipUserList(param);
			int vipLoginNum = 0;
			int userLoginNum = 0;
			if(!CollectionUtils.isEmpty(loginUserList) && !CollectionUtils.isEmpty(vipUserList)) {
				userLoginNum = loginUserList.size();
				Map<String, Integer> vipMap = new HashMap<>();
				for(String pin : vipUserList) {
					vipMap.put(pin, 1);
				}
				for (String pin: loginUserList) {
					vipLoginNum = vipLoginNum + (vipMap.get(pin) == null ? 0 : vipMap.get(pin));
				}
			}
			MonthActiveUsersReport report = new MonthActiveUsersReport();
			report.setDate(taskDate.substring(0, 7));
			report.setActiveUsersNum(userLoginNum);
			report.setActiveVipUsersNum(vipLoginNum);
			monthActiveUsersReportService.save(report);
		} catch (Exception e) {
			e.getMessage();
		}

	}


	public void init() {
		logger.info("init");
	}

	public void destroy() {
		logger.info("destory");
	}

}
